<template>
  <nav class="navbar navbar-dark bg-primary justify-content-between mb-4 px-4">
    <router-link class="navbar-brand" to="/">者也专栏</router-link>
    <!-- 没登陆的时候显示 登录和注册按钮 -->
    <ul v-if="!user.isLogin" class="list-inline mb-0">
      <!-- 改为routerlink 跳转路由链接 -->
      <li class="list-inline-item">
        <router-link to="/login" class="btn btn-outline-light my-2">登陆</router-link>
      </li>
      <li class="list-inline-item">
        <router-link to="/signup" class="btn btn-outline-light my-2">注册</router-link>
      </li>
    </ul>
    <!-- 登录了之后显示 用户的信息 -->
    <ul v-else class="list-inline mb-0">
      <li class="list-inline-item">
        <div class="dropdown-wrap" @mouseover="toggleOpen(true)" @mouseleave="toggleOpen(false)"
          :style="`width:100%;height:100%`">
          <dropdown :title="`你好 ${user.nickName}`" v-model:isOpen.sync="isOpen">
            <dropdown-item>
              <router-link to="/create"><a class="dropdown-item" href="#">新建文章</a></router-link>
            </dropdown-item>
            <dropdown-item>
              <router-link :to="`/column/${user.column}`"><a class="dropdown-item" href="#">我的专栏</a></router-link>
            </dropdown-item>
            <dropdown-item>
              <router-link :to="`/edit`"><a class="dropdown-item" href="#">编辑资料</a></router-link>
            </dropdown-item>
            <dropdown-item>
              <a class="dropdown-item" href="#" @click="handleLogout">退出登录</a>
            </dropdown-item>
          </dropdown>
        </div>
      </li>
    </ul>
  </nav>
</template>

<script lang="ts">
import Dropdown from "./DropdownBlock.vue";
import DropdownItem from "./DropdownItem.vue";
import { defineComponent, PropType, ref } from "vue";
import { useRouter } from "vue-router";
import store, { UserProps } from "../store";
import createMessage from "../components/createMessage";
// 定义规则：
// import { UserProps } from "@/store";
// 输出
export default defineComponent({
  name: "GlobalHeader",
  props: {
    user: {
      type: Object as PropType<UserProps>,
      required: true
    }
  },
  components: {
    Dropdown,
    DropdownItem
  },
  setup() {
    const isOpen = ref(false);
    const toggleOpen = (state: boolean) => {
      isOpen.value = state;
    };
    const router = useRouter();
    const handleLogout = () => {
      store.commit("logout");
      createMessage("退出登录成功", "success", 2000, "跳转到首页");
      setTimeout(() => {
        router.push("/");
      }, 2000);
    };
    return {
      isOpen,
      handleLogout,
      toggleOpen
    };
  }
});
</script>

<style lang="less">
a {
  text-decoration: none;

  &.dropdown-item {
    text-align: center;
  }
}
</style>
